// pages/answer/endAnswer/index.js
import {
  IMG_PATH
} from "../../../config/index"
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: 0,
    IMG_PATH: IMG_PATH,
    headUrl: `${IMG_PATH}/center/头像.png`,
    answer: [],
    num: 0,
    score: 0,
    userinfo: {},
    titleUrl: `${IMG_PATH}/answer/ydt.png`,
    bottomUrl: `${IMG_PATH}/answer/bot_cz.png`,
    leftFlowerUrl: `${IMG_PATH}/answer/lef_sz.png`,
    rightFlowerUrl: `${IMG_PATH}/answer/rig_sz.png`,
    isPopup: true,
    redImg: `${IMG_PATH}/answer/dhb_tc.png`,
    isActionAni:false,
    curTime:0,
    isAnimation: false, //动画是否正在执行
    isLeft: 1, //左右晃动,
    curLeft: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(app.globalData.answer, app.globalData.score)
    this.setData({
      statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
      num: options.num,
      answer: app.globalData.answer,
      score: app.globalData.score,
      userinfo: wx.getStorageSync('userinfo'),
    })
    this.decorAnimation();
  },
  /**
   * 装饰动画
   */
  decorAnimation() {
    this.data.isAnimation = true;
    this.animate('.rightimg', [{
        translateX: 0
      },
      {
        translateX: -200
      },
    ], 1000, function () {}.bind(this))
    this.animate('.leftimg', [{
        translateX: 0
      },
      {
        translateX: 200
      },
    ], 1000, function () {}.bind(this))
    this.decorAnimation1();
  },

  /**
   * 循环动画
   */
  decorAnimation1() {
    this.data.isLeft = -1 * this.data.isLeft
    this.animate(".bottomimg", [{
        translateX: this.data.curLeft
      },
      {
        translateX: this.data.isLeft * 20
      },
    ], 10000, function () {
      this.data.curLeft = this.data.isLeft * 20;
      if (this.data.isAnimation) {
        this.decorAnimation1();
      }
    }.bind(this))
   
  },
  /**
   * 返回上个页面
   */
  backClick() {
    wx.navigateBack()
  },
  toerr() {
    // wx.navigateToMiniProgram({
    //   appId: 'wxe8cec4b2f7c8e812',
    //   path: `src/pages/takeaway/index?inviteCode=damai&extid=${this.data.userinfo.openId}&extappid=wx892d877d9306f4bf&extpatch=pages/authorizedPage/authorizedPage`
    // })
    wx.navigateTo({
      url: '../errorAnswer/index',
    })
  },
  reopen() {
    wx.redirectTo({
      url: `/pages/firstpage/firstpage?isLogin=${0}`,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 红包缩小动画
   */
  redSmallAni() {
    this.animate('.red_img1', [{
        scale: [1, 1],
        top: "250rpx",
        left: "0rpx"
      },
      {
        scale: [0.15, 0.15],
        top: "600rpx",
        left: "290rpx"
      },
    ], 500, function () {
      this.setData({
        redImg: `${IMG_PATH}/answer/xhb_bg.png`
      },()=>{
        this.data.isActionAni = true;
        this.redShakeAni();
      })
    }.bind(this))
  },
  /**
   * 红包放大动画
   */
  redMaxAni() {
    this.animate('.red_img', [{
        scale: [1, 1]
      },
      {
        scale: [6, 6]
      },
    ], 500, function () {
      this.redMoveAni()
    }.bind(this))
  },
  /**
   * 红包图片加载完成
   */
  imgLoad(e) {
    this.redMaxAni();
  },

  /**
   * 红包移动动画
   */
  redMoveAni(){
    const time = setTimeout(() => {
      this.data.curTime =time;
      this.setData({
        isPopup: false
      }, () => {
        if(this.data.curTime){
          clearTimeout(time);
          this.data.curTime = 0;
          this.redSmallAni();
        }
      })
    }, 2000);
  },
  /**
   * 红包抖动动画
   */
  redShakeAni() {
    // console.log("111");
    this.animate('.red_img1', [{
        rotate:0,
        scale:[0.15, 0.15]
      },
      {
        rotate:15,
        scale:[0.15, 0.15]
      },
      {
        rotate:1,
        scale:[0.15, 0.15]
      },
      {
        rotate:-15,
        scale:[0.15, 0.15]
      },
      {
        rotate:0,
        scale:[0.15, 0.15]
      },
      {
        rotate:15,
        scale:[0.15, 0.15]
      },
      {
        rotate:1,
        scale:[0.15, 0.15]
      },
      {
        rotate:-15,
        scale:[0.15, 0.15]
      },
      {
        rotate:0,
        scale:[0.15, 0.15]
      }
    ], 500, function () {
      // this.clearAnimation('.red_img1', ()=> {
        // console.log(this.data.isActionAni);
        if(this.data.isActionAni){
          const time1 = setTimeout(()=>{
            // console.log(this.data.isActionAni,'1111');
            clearTimeout(time1);
            this.redShakeAni();
          },2000)  
        }
      // })
    }.bind(this))
  },
  /**
   * 红包跳转
   */
  redFun(){
    // console.log(this.data.userinfo.openId);
    wx.navigateToMiniProgram({
      appId: 'wxe8cec4b2f7c8e812',
      path: `src/pages/takeaway/index?inviteCode=damai&extid=${this.data.userinfo.openId}&extappid=wx892d877d9306f4bf&extpatch=pages/authorizedPage/authorizedPage`
    })
  },
  /**
   * 关闭红包页
   */
  closeRed(){
    if(this.data.curTime){
      this.setData({
        isPopup: false
      }, () => {
        if(this.data.curTime){
          clearTimeout(this.data.curTime);
          this.data.curTime = 0;
          this.redSmallAni();
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.isActionAni = true;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    // console.log(this.data.isActionAni);
    this.data.isActionAni = false;
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.data.isActionAni = false;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})